﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.IO;

namespace CDTM.Modules.DANHMUC.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.DM item = Components.DM.Load(this.ItemID);
                item.Delete();

                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Components.DM item = new Components.DM();
            if (this.ItemID > 0)
            {
                item = Components.DM.Load(this.ItemID);
            }

            Guid moduleID = new Guid(Request.QueryString["modeid"]);

            item.ModuleID = moduleID;

            item.CategoryID = int.Parse(ddlCategory.SelectedValue);

            //NoiDUng
            item.Name = txtName.Text.Trim();
            //Description
            item.Description = txtDescription.Text.Trim();
            //SortOrder
            item.SortOrder = txtSortOrder.Text != "" ? int.Parse(txtSortOrder.Text) : 0;
            //Kich hoat
            item.TrangThai = this.chkStatus.Checked;


            //Insert item
            if (this.ItemID == 0)
            {
                item.CreatedByUser = PortalSecurity.CurrentUser.Identity.Email;
                item.DateCreated = DateTime.Now;
                item.ModifiedByUser = PortalSecurity.CurrentUser.Identity.Email;
                item.DateModified = DateTime.Now;

                this.ItemID = item.Insert();
            }
            else
            {
                lblModifiedByUser.Text = PortalSecurity.CurrentUser.Identity.Email;
                lblDateModified.Text = DateTime.Now.ToString();

                item.ID = this.ItemID;
                item.Update();
            }

            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid moduleID = new Guid(Request.QueryString["modeid"]);

                ASPNETVN.PORTAL.Components.Share.Category.BuildDropDownListControlForAllID(ref ddlCategory, moduleID);

                if (this.ItemID > 0)
                {
                    Components.DM item = Components.DM.Load(this.ItemID);
                    if (item != null)
                    {
                        ddlCategory.SelectedValue = item.CategoryID.ToString();
                        txtName.Text = item.Name;
                        txtDescription.Text = item.Description;
                        txtSortOrder.Text = item.SortOrder.ToString();
                        chkStatus.Checked = item.TrangThai;
                        lblCreatedByUser.Text = item.CreatedByUser;
                        lblDataCreated.Text = item.DateCreated.ToString();
                        lblModifiedByUser.Text = item.ModifiedByUser;
                        lblDateModified.Text = item.DateModified.ToString();
                    }
                }
                else
                {
                    lblCreatedByUser.Text = PortalSecurity.CurrentUser.Identity.Email;
                    lblDataCreated.Text = DateTime.Now.ToString();

                    btnDelete.Visible = false;
                }
            }
        }

    }
}